package start.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import start.model.*;
import start.service.IBusinessService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class BusinessCtrl {
	@Resource
	private IBusinessService service;
	@RequestMapping("/business/get/{id}")
	public Business get(@PathVariable("id")int id) {
		return this.service.findById(id);
	}
	
	
	@RequestMapping("/business/search")
	public Map<String,Object> search(int page,int rows,String json){
		Map<String,Object> map = JSON.parseObject(json);
		if("".equals(map.get("bname"))) {
			map.remove("bname");
		}
		if("".equals(map.get("date1"))) {
			map.remove("date1");
		}
		if("".equals(map.get("date2"))) {
			map.remove("date2");
		}
		if("".equals(map.get("kind"))) {
			map.remove("kind");
		}
		if("".equals(map.get("province"))) {
			map.remove("province");
		}
		if("".equals(map.get("city"))) {
			map.remove("city");
		}
		PageHelper.startPage(page,rows);
		List<Business> list = this.service.findAll(map);
		long total=((Page<Business>)list).getTotal();
		Map<String,Object> data = new HashMap<String,Object>();
		data.put("rows",list);
		data.put("total", total);
		return data;
		
		
		
		
	
		
	}
	@RequestMapping("/business/query")
	public Map<String,Object> query(int page,int rows,String status){
		try {
			String array[];
			if(status.indexOf(",")>0){
				 array = status.split(",");
				
			}else {
				array=new String[]{status};
			}
			List<Integer> nums = new ArrayList<Integer>();
				for (String a : array) {
					nums.add(Integer.parseInt(a));
				}
			
			PageHelper.startPage(page,rows);
			List<Business> list = this.service.findAll(nums);
			long total=((Page<Business>)list).getTotal();
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("rows",list);
			map.put("total", total);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}
	@RequestMapping("/business/remove/{id}/{status}")
	public boolean remove(@PathVariable("id") int id,@PathVariable("status") int status) {
		System.out.println(status);
		try {
			Map<String,Integer> map = new HashMap<String,Integer>();
			map.put("id",id);
			map.put("status", status);
			
			this.service.remove(map);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
		
	}
	
	@RequestMapping("/business/save")
	public boolean save(String json) {
		try {
			
			Map<String,Object> map = JSON.parseObject(json);
			Integer hid = Integer.parseInt((String)map.get("hid"));
			HKind kind=new HKind();
			kind.setId(hid);
			Integer aid=Integer.parseInt((String)map.get("aid"));
			HAttr attr = new HAttr();
			attr.setId(aid);
			String pcode=(String)map.get("pcode");
			Province pro = new Province();
			pro.setPcode(pcode);
			String tcode=(String)map.get("tcode");
			City city = new City();
			city.setCtcode(tcode);
			String ycode=(String)map.get("ycode");
			County county = new County();
			county.setCycode(ycode);
			Integer sid=Integer.parseInt((String)map.get("sid"));
			Street street = new Street();
			street.setId(sid);
			
			
			
			
			
			Business bean=new Business();
			bean.setKind(kind);
			bean.setAttr(attr);
			bean.setProvince(pro);
			bean.setCity(city);
			bean.setCounty(county);
			bean.setStreet(street);
			bean.setBname((String)map.get("bname"));
			bean.setFr((String)map.get("fr"));
			bean.setPhone((String)map.get("phone"));
			bean.setTel((String)map.get("tel"));
			bean.setZyyw((String)map.get("zyyw"));
			bean.setYyzz((String)map.get("yyzz"));
			bean.setAddress((String)map.get("address"));
			bean.setInfo((String)map.get("info"));
			bean.setLogo((String)map.get("logo"));
			
			
			this.service.save(bean);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
		
	}	
		
		
	
	
	
}
